SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE proc [dbo].[SP_TASALD_EMPR_Q30]            
/*---------------------------------------------------------------------*/            
/*----- Empresa          : OFISIS S.A.                            -----*/            
/*----- Cliente          : OFISIS S.A.                             ----*/            
/*----- Sistema          : Contabilidad                           -----*/            
/*----- Modulo           : Empresa                                -----*/            
/*----- Programa         : Balance de Comprobacion - RS 034-2006  -----*/            
/*----- Nombre           : SP_TASALD_EMPR_Q30                     -----*/            
/*----- Script           : TASALD_EMPR                            -----*/            
/*----- Desarrollado     : Franco Gamboni Koifman                 -----*/            
/*----- Fecha            : 15/10/2002                             -----*/            
/*----- Base de Datos    : MS SQL Server                          -----*/            
/*----- Version BD       : 9.0                                    -----*/            
/*----- Invoca a SP      :                                        -----*/            
/*---------------------------------------------------------------------*/            
/*---------------------------------------------------------------------*/            
/*----- Modificado 1 por : LMGP                                   -----*/            
/*----- Fecha Modifica   : 04/09/2008                             -----*/            
/*----- Detalle Modifica : Reporte con Filtro                     -----*/            
/*---------------------------------------------------------------------*/            
/*---------------------------------------------------------------------*/            
/*----- Drop Proc SP_TASALD_EMPR_Q30                              -----*/            
/*----- Grant Execute On SP_TASALD_EMPR_Q30 To PUBLIC             -----*/            
/*----- SP_TASALD_EMPR_Q30                                     -----*/            
/*---------------------------------------------------------------------            
 EXEC SP_TASALD_EMPR_Q30 '01', 1, 2002, 2, 'M', 'S', 1, '01211' , '14102' , 5, 'TAB'            
 EXEC SP_TASALD_EMPR_Q30 '01', 1, 2008, 10, 'A', 'N', 1, NULL , NULL , 7, 'REP', 'M', ''          
---------------------------------------------------------------------*/            
@ISCO_EMPR      TD_VC_002,            
@INNU_CNTB_EMPR TD_IN_001_CONT,            
@INNU_ANNO      TD_IN_004_ANNO,            
@INNU_MESE      TD_IN_002_MESE,            
@ISTI_BALA      TD_VC_001,      --Tipo Balance (M)ensual/(A)cumulado            
@ISST_QUIE      TD_VC_001,      --Con Quiebre (S/N)            
@INNV_PRES      TD_IN_001,      --Nivel Presentacion 1 / 1000 / 1000000            
@ISCO_CNTA_INIC TD_VC_020_CNTA,             
@ISCO_CNTA_FINA TD_VC_020_CNTA,             
@INNU_DGTO      TD_IN_001,            
@ISTI_PRES      TD_VC_003,       -- Tipo Presentacion REP / TAB            
@ISTI_MONT      TD_VC_001,       -- M = Fila con Monto, T = Todas las Filas            
@ISCA_WHER      TD_VC_255        
  
As            
Declare            
@CSCO_CNTA      TD_VC_020_CNTA,             
@CSNO_CNTA      TD_VC_050,            
@CNIM_CARG_ANTE TD_NU_016_004,            
@CNIM_ABON_ANTE TD_NU_016_004,            
@CSCO_BALA      TD_VC_003,            
@VSNV_PRES      TD_VC_001,            
@VSCO_CNTA      TD_VC_002,            
@VSCO_BALA      TD_VC_002,            
@CNIM_CARG_MESE TD_NU_016_004,            
@CNIM_ABON_MESE TD_NU_016_004,            
@VNTO_CALC      TD_NU_016_004,            
@VSNU_CNTB_EMPR TD_VC_001,            
@VSNU_ANNO      TD_VC_004,            
@VSNU_MESE      TD_VC_002,            
@VSNU_DGTO      TD_VC_002,            
@VSWH_CNTA      TD_VC_255,            
@VNNV_0001   TD_IN_001,            
@VNNV_0002   TD_IN_001,            
@VNNV_0003   TD_IN_001,            
@VNNV_0004   TD_IN_001,            
@VNNV_0005   TD_IN_001,            
@VNNV_0006   TD_IN_001,            
@VNNV_0007   TD_IN_001,            
@VNNV_0008   TD_IN_001,            
@INNU_POSI      TD_IN_001,            
@VSCA_SELE_0001 char(800),            
@VSCA_SELE_0002 char(800)            
            
Create Table #TWSALD_EMPR_Q30_1(            
CO_CNTA_EMPR Char(10) NULL,       
DE_CNTA_EMPR Char(50) NULL,       
IM_CARG_ANTE Numeric(16,4) NULL,       
IM_ABON_ANTE Numeric(16,4) NULL,       
IM_CARG_MESE Numeric(16,4) NULL,       
IM_ABON_MESE Numeric(16,4) NULL,       
IM_SALD_CARG Numeric(16,4) NULL,       
IM_SALD_ABON Numeric(16,4) NULL,       
IM_ACTI_INVE Numeric(16,4) NULL,       
IM_PASI_INVE Numeric(16,4) NULL,       
IM_PERD_FUNC Numeric(16,4) NULL,       
IM_GANA_FUNC Numeric(16,4) NULL)       
      
Select @VSNU_CNTB_EMPR = Convert(Varchar(1),@INNU_CNTB_EMPR)       
Select @VSNU_ANNO = Convert(Varchar(4),@INNU_ANNO )       
Select @VSNU_MESE = Convert(Varchar(2),@INNU_MESE )       
Select @VSNU_DGTO = Convert(Varchar(2),@INNU_DGTO )       
Select @VSWH_CNTA = ''       
  
If Datalength( IsNull(@ISCO_CNTA_INIC,'')) > 0       
 Select @VSWH_CNTA = ' And TMPLAN_EMPR.CO_CNTA_EMPR >= "' + @ISCO_CNTA_INIC + '" And TMPLAN_EMPR.CO_CNTA_EMPR <= "' + @ISCO_CNTA_FINA + '"'       
  
If @ISTI_BALA = 'M'      
Exec( ' Declare CU_TASALD_EMPR_Q30_1 Cursor For             
 Select  Rtrim(TMPLAN_EMPR.CO_CNTA_EMPR),  MAX(RTRIM(TMPLAN_EMPR.DE_CNTA_EMPR)),             
 Isnull(Sum(Round(Isnull(TASALD_EMPR.IM_CARG_CNTB,0),2)), 0),       
 Isnull(Sum(Round(Isnull(TASALD_EMPR.IM_ABON_CNTB,0),2)), 0),             
 TMPLAN_EMPR.CO_BALA, Max(T4.IM_CARG_MESE), Max(T4.IM_ABON_MESE)             
 From TMPLAN_EMPR  Left Outer Join TASALD_EMPR        
 On TASALD_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"         
 ' + @ISCA_WHER + '         
 And Convert(Varchar(2), TASALD_EMPR.NU_CNTB_EMPR) = "' +@VSNU_CNTB_EMPR+ '"         
 And Convert(Varchar(4), TASALD_EMPR.NU_ANNO) = "' +@VSNU_ANNO+ '"         
 And TASALD_EMPR.NU_MESE < '+ @VSNU_MESE + '  
 And TASALD_EMPR.TI_AUXI_EMPR IS NULL         
 And TASALD_EMPR.CO_AUXI_EMPR IS NULL         
 And TASALD_EMPR.CO_ORDE_SERV IS NULL         
 And TASALD_EMPR.CO_EMPR = TMPLAN_EMPR.CO_EMPR         
 And TASALD_EMPR.CO_CNTA_EMPR = TMPLAN_EMPR.CO_CNTA_EMPR         
 Left Outer Join (Select TASALD_EMPR.CO_CNTA_EMPR,         
           ISNULL(SUM(ROUND(ISNULL(TASALD_EMPR.IM_CARG_CNTB, 0),2)),0) As IM_CARG_MESE,         
           ISNULL(SUM(ROUND(ISNULL(TASALD_EMPR.IM_ABON_CNTB, 0),2)),0) As IM_ABON_MESE          
    From   TASALD_EMPR         
    Where  TASALD_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"         
    And    Convert(Varchar(2),TASALD_EMPR.NU_CNTB_EMPR) = "' +@VSNU_CNTB_EMPR+ '"            
    And    Convert(Varchar(4),TASALD_EMPR.NU_ANNO) = "'+@VSNU_ANNO+ '"            
    And    TASALD_EMPR.NU_MESE = '+@VSNU_MESE+ '            
    And    TASALD_EMPR.TI_AUXI_EMPR IS NULL             
    And    TASALD_EMPR.CO_AUXI_EMPR IS NULL             
    And    TASALD_EMPR.CO_ORDE_SERV IS NULL         
    ' + @ISCA_WHER + '        
    Group By TASALD_EMPR.CO_CNTA_EMPR  ) T4             
 On T4.CO_CNTA_EMPR = TMPLAN_EMPR.CO_CNTA_EMPR         
 Where TMPLAN_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"          
 And Convert(Varchar(2),Datalength(TMPLAN_EMPR.CO_CNTA_EMPR)) = "' +@VSNU_DGTO+ '" '  +  @VSWH_CNTA +  '         
Group By TMPLAN_EMPR.CO_CNTA_EMPR, TMPLAN_EMPR.CO_BALA             
Order By 1' )            
  
Else If @ISTI_BALA = 'A'      
Exec( ' Declare CU_TASALD_EMPR_Q30_1 Cursor For             
 Select  Rtrim(TMPLAN_EMPR.CO_CNTA_EMPR),  MAX(RTRIM(TMPLAN_EMPR.DE_CNTA_EMPR)),             
 Isnull(Sum(Round(Isnull(TASALD_EMPR.IM_CARG_CNTB,0),2)), 0),       
 Isnull(Sum(Round(Isnull(TASALD_EMPR.IM_ABON_CNTB,0),2)), 0),             
 TMPLAN_EMPR.CO_BALA, Max(T4.IM_CARG_MESE), Max(T4.IM_ABON_MESE)             
 From TMPLAN_EMPR  Left Outer Join TASALD_EMPR        
 On TASALD_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"  
 ' + @ISCA_WHER + '  
 And TASALD_EMPR.NU_CNTB_EMPR = ' +@VSNU_CNTB_EMPR+ '  
 And TASALD_EMPR.NU_ANNO = ' +@VSNU_ANNO+ '  
 And TASALD_EMPR.NU_MESE = 0  
 And TASALD_EMPR.TI_AUXI_EMPR IS NULL  
 And TASALD_EMPR.CO_AUXI_EMPR IS NULL  
 And TASALD_EMPR.CO_ORDE_SERV IS NULL  
 And TASALD_EMPR.CO_EMPR = TMPLAN_EMPR.CO_EMPR  
 And TASALD_EMPR.CO_CNTA_EMPR = TMPLAN_EMPR.CO_CNTA_EMPR  
 Left Outer Join (Select TASALD_EMPR.CO_CNTA_EMPR,         
           ISNULL(SUM(ROUND(ISNULL(TASALD_EMPR.IM_CARG_CNTB, 0),2)),0) As IM_CARG_MESE,         
           ISNULL(SUM(ROUND(ISNULL(TASALD_EMPR.IM_ABON_CNTB, 0),2)),0) As IM_ABON_MESE          
    From   TASALD_EMPR         
    Where  TASALD_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"         
    And    TASALD_EMPR.NU_CNTB_EMPR = ' +@VSNU_CNTB_EMPR+ '  
    And    TASALD_EMPR.NU_ANNO = '+@VSNU_ANNO+ '  
    And    TASALD_EMPR.NU_MESE > 0  
    And    TASALD_EMPR.NU_MESE <= ' + @VSNU_MESE + '  
    And    TASALD_EMPR.TI_AUXI_EMPR IS NULL             
    And    TASALD_EMPR.CO_AUXI_EMPR IS NULL             
    And    TASALD_EMPR.CO_ORDE_SERV IS NULL         
    ' + @ISCA_WHER + '        
    Group By TASALD_EMPR.CO_CNTA_EMPR  ) T4             
 On T4.CO_CNTA_EMPR = TMPLAN_EMPR.CO_CNTA_EMPR         
 Where TMPLAN_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"          
 And Convert(Varchar(2),Datalength(TMPLAN_EMPR.CO_CNTA_EMPR)) = "' +@VSNU_DGTO+ '" '  +  @VSWH_CNTA +  '         
Group By TMPLAN_EMPR.CO_CNTA_EMPR, TMPLAN_EMPR.CO_BALA             
Order By 1' )          
  
Open CU_TASALD_EMPR_Q30_1            
Fetch CU_TASALD_EMPR_Q30_1 Into @CSCO_CNTA, @CSNO_CNTA, @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CSCO_BALA,             
      @CNIM_CARG_MESE, @CNIM_ABON_MESE            
  
While @@Fetch_status = 0            
Begin            
  
Select @VSCO_CNTA = SUBSTRING(@CSCO_CNTA,1,2)  
Select @VSCO_BALA = SUBSTRING(@CSCO_BALA,1,2)  
  
If @CNIM_CARG_MESE IS NULL  
 Begin  
  Select @CNIM_CARG_MESE = 0  
 End  
  
If @CNIM_ABON_MESE IS NULL  
 Begin  
  Select @CNIM_ABON_MESE = 0  
 End  
        
if @CNIM_CARG_ANTE IS NULL         
 Begin         
  Select @CNIM_CARG_ANTE = 0        
 End         
      
if @CNIM_ABON_ANTE IS NULL         
 Begin         
  Select @CNIM_ABON_ANTE = 0        
 End         
            
 If @ISTI_BALA = 'M'            
   Select @VNTO_CALC = ROUND(@CNIM_CARG_MESE,2) - ROUND(@CNIM_ABON_MESE,2)            
 Else If @ISTI_BALA = 'A'            
   Select @VNTO_CALC = ROUND(@CNIM_CARG_ANTE,2) - ROUND(@CNIM_ABON_ANTE,2) + ROUND(@CNIM_CARG_MESE,2) - ROUND(@CNIM_ABON_MESE,2)            
        
 If @VNTO_CALC IS NULL         
  Begin         
   Select @VNTO_CALC = 0        
  End         
            
 /*----------------------------- GANANCIAS ------------------------------------*/            
 If @VNTO_CALC >= 0            
 Begin            
  /*------- Cuentas de Inventario : Activo ----------*/            
  If @VSCO_CNTA <'60'            
  Begin             
   Insert #TWSALD_EMPR_Q30_1(            
   CO_CNTA_EMPR, DE_CNTA_EMPR, IM_CARG_ANTE, IM_ABON_ANTE, IM_CARG_MESE, IM_ABON_MESE, IM_SALD_CARG,            
   IM_SALD_ABON, IM_ACTI_INVE, IM_PASI_INVE, IM_PERD_FUNC, IM_GANA_FUNC )            
   Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CNIM_CARG_MESE, @CNIM_ABON_MESE,            
   @VNTO_CALC, 0, @VNTO_CALC, 0, 0, 0 )            
  End            
  /*------- Perdida por Funcion ---------*/            
  Else If @VSCO_BALA in ('01', '11')  
  Begin       
   Insert #TWSALD_EMPR_Q30_1(            
   CO_CNTA_EMPR, DE_CNTA_EMPR, IM_CARG_ANTE, IM_ABON_ANTE, IM_CARG_MESE, IM_ABON_MESE, IM_SALD_CARG,            
   IM_SALD_ABON, IM_ACTI_INVE, IM_PASI_INVE, IM_PERD_FUNC, IM_GANA_FUNC )            
   Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CNIM_CARG_MESE, @CNIM_ABON_MESE,            
   @VNTO_CALC, 0, 0, 0, @VNTO_CALC, 0)            
  End             
  Else            
  Begin            
   Insert #TWSALD_EMPR_Q30_1(            
   CO_CNTA_EMPR, DE_CNTA_EMPR, IM_CARG_ANTE, IM_ABON_ANTE, IM_CARG_MESE, IM_ABON_MESE, IM_SALD_CARG,            
   IM_SALD_ABON, IM_ACTI_INVE, IM_PASI_INVE, IM_PERD_FUNC, IM_GANA_FUNC )            
   Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CNIM_CARG_MESE, @CNIM_ABON_MESE,            
   @VNTO_CALC, 0, 0, 0, 0, 0 )            
  End             
 End --( If @VNTO_CALC >= 0 )            
 /*----------------------------- PERDIDAS ------------------------------------*/            
 Else If @VNTO_CALC < 0            
 Begin            
  /*------- Cuentas de Inventario : Pasivo ----------*/            
  If @VSCO_CNTA <'60'            
  Begin             
   Insert #TWSALD_EMPR_Q30_1(            
   CO_CNTA_EMPR, DE_CNTA_EMPR, IM_CARG_ANTE, IM_ABON_ANTE, IM_CARG_MESE, IM_ABON_MESE, IM_SALD_CARG,            
   IM_SALD_ABON, IM_ACTI_INVE, IM_PASI_INVE, IM_PERD_FUNC, IM_GANA_FUNC )            
   Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CNIM_CARG_MESE, @CNIM_ABON_MESE,            
   0, Abs(@VNTO_CALC), 0, Abs(@VNTO_CALC), 0, 0 )            
  End            
  /*------- Ganancia por Funcion ---------*/            
  Else If @VSCO_BALA in ('01', '11')            
  Begin             
   Insert #TWSALD_EMPR_Q30_1(            
   CO_CNTA_EMPR, DE_CNTA_EMPR, IM_CARG_ANTE, IM_ABON_ANTE, IM_CARG_MESE, IM_ABON_MESE, IM_SALD_CARG,            
   IM_SALD_ABON, IM_ACTI_INVE, IM_PASI_INVE, IM_PERD_FUNC, IM_GANA_FUNC )            
   Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CNIM_CARG_MESE, @CNIM_ABON_MESE,            
   0, Abs(@VNTO_CALC), 0, 0, 0, Abs(@VNTO_CALC) )            
  End              
  Else            
  Begin            
   Insert #TWSALD_EMPR_Q30_1(            
   CO_CNTA_EMPR, DE_CNTA_EMPR, IM_CARG_ANTE, IM_ABON_ANTE, IM_CARG_MESE, IM_ABON_MESE, IM_SALD_CARG,            
   IM_SALD_ABON, IM_ACTI_INVE, IM_PASI_INVE, IM_PERD_FUNC, IM_GANA_FUNC )            
   Values (RTRIM(@CSCO_CNTA), RTRIM(@CSNO_CNTA), @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CNIM_CARG_MESE, @CNIM_ABON_MESE,            
   0, Abs(@VNTO_CALC), 0, 0, 0 ,0 )            
  End             
 End --( If @VNTO_CALC < 0 )            
           
Fetch CU_TASALD_EMPR_Q30_1 Into @CSCO_CNTA, @CSNO_CNTA, @CNIM_CARG_ANTE, @CNIM_ABON_ANTE, @CSCO_BALA,             
      @CNIM_CARG_MESE, @CNIM_ABON_MESE            
End            
            
Close CU_TASALD_EMPR_Q30_1            
Deallocate CU_TASALD_EMPR_Q30_1            
            
If @ISTI_MONT = 'T'            
Begin             
  If @ISST_QUIE = 'N'            
  Begin            
   If @ISTI_PRES = 'REP'            
   Begin            
    Select CO_CNTA_EMPR, DE_CNTA_EMPR,             
    Convert(Numeric(16,4),Round(IM_CARG_ANTE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_ANTE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_CARG_MESE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_MESE/@INNV_PRES,2)),       
    Convert(Numeric(16,4),Round(IM_SALD_CARG/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_SALD_ABON/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_ACTI_INVE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_PASI_INVE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_PERD_FUNC/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_GANA_FUNC/@INNV_PRES,2)),            
    0,0,0,0,0,0,0,0,0,0,1,0            
    From #TWSALD_EMPR_Q30_1            
    Order By 1            
            
   End            
   Else If @ISTI_PRES = 'TAB'            
   Begin            
            
    Select CO_CNTA_EMPR, DE_CNTA_EMPR,             
    Convert(Numeric(16,4),Round(IM_CARG_ANTE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_ANTE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_CARG_MESE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_MESE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_SALD_CARG/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_SALD_ABON/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_ACTI_INVE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_PASI_INVE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_PERD_FUNC/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_GANA_FUNC/@INNV_PRES,2))            
    From #TWSALD_EMPR_Q30_1            
    Order By 1            
            
End            
  End --( If @ISST_QUIE = 'N' )            
  Else If @ISST_QUIE = 'S'            
  Begin            
   If @ISTI_PRES = 'REP'            
   Begin            
    Select CO_CNTA_EMPR, DE_CNTA_EMPR,             
    Convert( Numeric(16,4), Round(IM_CARG_ANTE/@INNV_PRES,2)), Convert(Numeric(16,4), Round(IM_ABON_ANTE/@INNV_PRES,2)),      
 Convert( Numeric(16,4), Round(IM_CARG_MESE/@INNV_PRES,2)), Convert(Numeric(16,4), Round(IM_ABON_MESE/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_SALD_CARG/@INNV_PRES,2)), Convert(Numeric(16,4), Round(IM_SALD_ABON/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_ACTI_INVE/@INNV_PRES,2)), Convert(Numeric(16,4), Round(IM_PASI_INVE/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_PERD_FUNC/@INNV_PRES,2)), Convert(Numeric(16,4), Round(IM_GANA_FUNC/@INNV_PRES,2)),      
    0,0,0,0,0,0,0,0,0,0,1,0            
    From #TWSALD_EMPR_Q30_1            
    Union All            
    Select t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR,             
    0,0,0,0,0,0,0,0,0,0,            
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_CARG,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_ABON,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ACTI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PASI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PERD_FUNC,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_GANA_FUNC,0)/@INNV_PRES,2))),            
    0,1            
    From #TWSALD_EMPR_Q30_1 t1, TMPLAN_EMPR t2            
    Where t2.CO_EMPR = @ISCO_EMPR            
    And   t2.NU_ANNO = 9999            
    And   Datalength(t2.CO_CNTA_EMPR) < @INNU_DGTO            
    And   Substring(t1.CO_CNTA_EMPR,1, Datalength(t2.CO_CNTA_EMPR) ) = t2.CO_CNTA_EMPR            
    Group By t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR            
    Order By 1            
   End            
   Else If @ISTI_PRES = 'TAB'            
   Begin            
    Select t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR,             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_CARG,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_ABON,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ACTI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PASI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PERD_FUNC,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_GANA_FUNC,0)/@INNV_PRES,2)))            
    From #TWSALD_EMPR_Q30_1 t1, TMPLAN_EMPR t2            
    Where t2.CO_EMPR = @ISCO_EMPR            
    And   t2.NU_ANNO = 9999            
    And   Datalength(t2.CO_CNTA_EMPR) <= @INNU_DGTO            
    And Substring(t1.CO_CNTA_EMPR,1, Datalength(t2.CO_CNTA_EMPR) ) = t2.CO_CNTA_EMPR            
    Group By t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR            
    Order By 1            
   End            
  End --(Else If @ISST_QUIE = 'S')            
End  -- (If @ISTI_MONT = 'T')            
Else If @ISTI_MONT = 'M'            
Begin             
  If @ISST_QUIE = 'N'            
  Begin            
   If @ISTI_PRES = 'REP'            
   Begin            
    Select CO_CNTA_EMPR, DE_CNTA_EMPR,             
    Convert(Numeric(16,4),Round(IM_CARG_ANTE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_ANTE/@INNV_PRES,2)),      
 Convert(Numeric(16,4),Round(IM_CARG_MESE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_MESE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_SALD_CARG/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_SALD_ABON/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_ACTI_INVE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_PASI_INVE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_PERD_FUNC/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_GANA_FUNC/@INNV_PRES,2)),            
    0,0,0,0,0,0,0,0,0,0,1,0       
    From #TWSALD_EMPR_Q30_1            
    Where Abs(IM_CARG_ANTE) > 0 Or Abs(IM_ABON_ANTE) > 0 Or Abs(IM_CARG_MESE) > 0 Or Abs(IM_ABON_MESE) > 0 Or       
       Abs(IM_SALD_CARG) > 0 Or Abs(IM_SALD_ABON) > 0             
    Order By CO_CNTA_EMPR            
            
   End            
   Else If @ISTI_PRES = 'TAB'            
   Begin            
    Select CO_CNTA_EMPR, DE_CNTA_EMPR,             
    Convert(Numeric(16,4),Round(IM_CARG_ANTE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_ANTE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_CARG_MESE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_ABON_MESE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_SALD_CARG/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_SALD_ABON/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_ACTI_INVE/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_PASI_INVE/@INNV_PRES,2)),       
 Convert(Numeric(16,4),Round(IM_PERD_FUNC/@INNV_PRES,2)), Convert(Numeric(16,4),Round(IM_GANA_FUNC/@INNV_PRES,2))            
    From #TWSALD_EMPR_Q30_1            
    Where Abs(IM_CARG_ANTE) > 0 or Abs(IM_ABON_ANTE) > 0 or Abs(IM_CARG_MESE) > 0 or Abs(IM_ABON_MESE) > 0 Or       
    Abs(IM_SALD_CARG) > 0 or Abs(IM_SALD_ABON) > 0            
    Order By 1            
            
   End            
  End --( If @ISST_QUIE = 'N' )            
  Else If @ISST_QUIE = 'S'            
  Begin            
   If @ISTI_PRES = 'REP'            
   Begin            
    Select CO_CNTA_EMPR, DE_CNTA_EMPR,             
    Convert( Numeric(16,4), Round(IM_CARG_ANTE/@INNV_PRES,2)), Convert( Numeric(16,4), Round(IM_ABON_ANTE/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_CARG_MESE/@INNV_PRES,2)), Convert( Numeric(16,4), Round(IM_ABON_MESE/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_SALD_CARG/@INNV_PRES,2)), Convert( Numeric(16,4), Round(IM_SALD_ABON/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_ACTI_INVE/@INNV_PRES,2)), Convert( Numeric(16,4), Round(IM_PASI_INVE/@INNV_PRES,2)),       
 Convert( Numeric(16,4), Round(IM_PERD_FUNC/@INNV_PRES,2)), Convert( Numeric(16,4), Round(IM_GANA_FUNC/@INNV_PRES,2)),            
    0,0,0,0,0,0,0,0,0,0,1,0       
    From #TWSALD_EMPR_Q30_1            
    Where (Abs(IM_CARG_ANTE) > 0 or Abs(IM_ABON_ANTE) > 0 or Abs(IM_CARG_MESE) > 0 or Abs(IM_ABON_MESE) > 0             
       or Abs(IM_SALD_CARG) > 0 or Abs(IM_SALD_ABON) > 0 )      
    Union All            
    Select t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR,             
    0,0,0,0,0,0,0,0,0,0,      
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_ANTE,0)/@INNV_PRES,2))),      
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_CARG,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_ABON,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ACTI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PASI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PERD_FUNC,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_GANA_FUNC,0)/@INNV_PRES,2))),      
    0,1            
    From #TWSALD_EMPR_Q30_1 t1, TMPLAN_EMPR t2            
    Where t2.CO_EMPR = @ISCO_EMPR            
    And   t2.NU_ANNO = 9999            
    And   Datalength(t2.CO_CNTA_EMPR) < @INNU_DGTO            
    And   Substring(t1.CO_CNTA_EMPR,1, Datalength(t2.CO_CNTA_EMPR) ) = t2.CO_CNTA_EMPR            
    And   (Abs(t1.IM_CARG_ANTE) > 0 or Abs(t1.IM_ABON_ANTE) > 0 or Abs(t1.IM_CARG_MESE) > 0 or Abs(t1.IM_ABON_MESE) > 0             
        or Abs(t1.IM_SALD_CARG) > 0 or Abs(t1.IM_SALD_ABON) > 0 )            
    Group By t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR            
    Order By 1            
   End            
   Else If @ISTI_PRES = 'TAB'            
   Begin            
    Select t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR,             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_ANTE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_CARG_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ABON_MESE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_CARG,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_SALD_ABON,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_ACTI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PASI_INVE,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_PERD_FUNC,0)/@INNV_PRES,2))),             
    Convert( Numeric(16,4), Sum(Round(IsNull(t1.IM_GANA_FUNC,0)/@INNV_PRES,2)))            
    From #TWSALD_EMPR_Q30_1 t1, TMPLAN_EMPR t2            
    Where t2.CO_EMPR = @ISCO_EMPR            
    And   t2.NU_ANNO = 9999            
    And   Datalength(t2.CO_CNTA_EMPR) <= @INNU_DGTO      
    And   Substring(t1.CO_CNTA_EMPR,1, Datalength(t2.CO_CNTA_EMPR) ) = t2.CO_CNTA_EMPR       
    And   (Abs(t1.IM_CARG_ANTE) > 0 or Abs(t1.IM_ABON_ANTE) > 0 or Abs(t1.IM_CARG_MESE) > 0 or Abs(t1.IM_ABON_MESE) > 0      
        or Abs(t1.IM_SALD_CARG) > 0 or Abs(t1.IM_SALD_ABON)> 0)      
    Group By t2.CO_CNTA_EMPR, t2.DE_CNTA_EMPR      
    Order By 1      
   End            
  End --(Else If @ISST_QUIE = 'S')            
End  -- (If @ISTI_MONT = 'M')            
/*------------------------------------- Fin --------------------------------------------------------*/            
        
        
        
        
      
    
  

GO
